<script setup lang="ts">
import {ref,onBeforeMount} from "vue";
import {queryProductList} from "@/api/product.js"
//分页信息
const pageSize = ref(10)
const page = ref(1)
const total = ref(0)

//绑定表单数据
const tableData = ref([]);

//选择的数据
const chooseTable = ref();

//
const productname = ref();

//获取父组件传来的数据
const props = defineProps({
  visible:{
    type: Boolean,
    default: false
  }
});

//声明时间让父组件监听
const emits = defineEmits(["close", "choose"]);

//触发关闭事件
function close() {
  emits("close");
}

//重置搜索条件
function resset(){
  (productname.value="")
  initData()
}
//搜索事件
async function initData() {
  //定义查询条件
  let data = {
    page: page.value,
    pageSize: pageSize.value,
    productname: productname.value
  }
  //搜索
  let res = await queryProductList(data);
  if (res.code == 200){
    //把获取的内容拿出来
    tableData.value = res.data.data;
    page.value = res.data.page;
    pageSize.value = res.data.pageSize;
    total.value = res.data.total;
  }
}

//当前页变化分页变化
function chgpg(pg){
  //当前页变化
  page.value = pg;
  //刷新数据
  initData();
}

//每页记录数变化
function chgsize(size){
  //每页记录数变化
  pageSize.value = size;
  //当前页变为第一页
  page.value = 1
  //刷新数据
  initData();
}
//选择产品
function saveDict(){
  //获取选中的行
  let rows = chooseTable.value.getSelectionRows();
  //触发选择事件 将数据传递给父组件
  emits("choose", rows);
  close();
}


onBeforeMount(()=>{
  initData()
})
</script>

<template>
  <el-dialog
    :model-value="visible"
    title="选择产品"
    width="800"
    :close-on-click-modal="false"
    @close="close"
  >
    <div>
      <el-input
        v-model="productname"
        placeholder="请输入产品名称"
        style="width: 200px"
      />
      <el-button type="primary" @click="initData">搜索</el-button>
      <el-button type="info" @click="resset">重置</el-button>
    </div>

    <!--客户列表-->
    <el-table
      ref="chooseTable"
      :data="tableData"
      style="width: 100%"
      stripe
      border
    >
      <el-table-column
        type="selection"
        width="80"
        header-align="center"
        align="center"
      />
      <el-table-column
        prop="productnumber"
        label="产品编号"
        width="180"
        header-align="center"
      />
      <el-table-column prop="name" label="产品名称" header-align="center" />
      <el-table-column
        prop="productunit"
        label="产品单位"
        width="180"
        header-align="center"
      />
      <el-table-column
        prop="producttype"
        label="产品类型"
        width="180"
        header-align="center"
      />
    </el-table>
    <!--分页-->
    <div
      style="
        background-color: #fff;
        height: 50px;
        line-height: 50px;
        text-align: right;
      "
    >
      <el-pagination
        background
        :page-size="pageSize"
        layout="total,sizes,prev, pager, next,jumper"
        :total="total"
        @size-change="chgsize"
        @current-change="chgpg"
      />
    </div>

    <div style="text-align: right">
      <el-button type="primary" @click="saveDict">选择</el-button>
      <el-button type="info" @click="close">关闭</el-button>
    </div>
  </el-dialog>
</template>

<style scoped lang="scss">


</style>
